package com.socool.sinofaith.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.dao.QuestionAnswerDao;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.AnswerReq;
import com.socool.sinofaith.model.BaseMessage;
import com.socool.sinofaith.model.QuestionAnswerBean;
import com.socool.sinofaith.model.QuestionAnswerReqBean;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.S;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.LoadingDialog;
import com.socool.sinofaith.view.MyDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

public class AnswerFinishActivity extends BaseActivity
		implements CenterViewFactory, SpinnerViewFactory, View.OnClickListener {
	ActionBarHelperBase mActionBarHelperBase;

	private Button btnPrevious, btnNext;

	private String Task_Name, tempjson, end;

	private QuestionAnswerReqBean answerReqBean;

	private QuestionAnswerBean answerBean;

	private LoadingDialog mLoadingDialog;

	private int count, totalcount, failcount, failtotalcount = 0;

	private List<HashMap<String, String>> paramsList = new ArrayList<HashMap<String, String>>();

	Dialog dialog;

	Lock lock = new ReentrantLock();

	private String userMobile;
	HashMap<String, String> paramsMap = new HashMap<String, String>();
	HashMap<String, String> firstParamsMap = new HashMap<String, String>();

	int fcount = 0;
	String qtype = "1";
	String fileType = "";

	int success;
	int sum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_answerfinish);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		mActionBarHelperBase.setmClickListener(this);

		mLoadingDialog = new LoadingDialog(this);

		Task_Name = getIntent().getStringExtra("Task_Name");
		answerReqBean = (QuestionAnswerReqBean) getIntent().getSerializableExtra("answerReqBean");
		answerBean = (QuestionAnswerBean) getIntent().getSerializableExtra("answerBean");
		userMobile = getSPString();
		btnPrevious = (Button) findViewById(R.id.btnPrevious);
		btnNext = (Button) findViewById(R.id.btnNext);

		btnPrevious.setOnClickListener(this);
		btnNext.setOnClickListener(this);

		if (null != SinofaithUtil.batchAnswer && SinofaithUtil.batchAnswer.size() > 0) {
			btnNext.setVisibility(View.GONE);
		} else {
			btnNext.setVisibility(View.VISIBLE);
		}

		try {
			if (null != answerReqBean.getSubAnswer() && answerReqBean.getSubAnswer().size() > 0) {
				for (int i = 0; i < answerReqBean.getSubAnswer().size(); i++) {
					AnswerReq answerReq = answerReqBean.getSubAnswer().get(i);
					if (null != answerReq.getFileCount() && !"".equals(answerReq.getFileCount())
							&& !"0".equals(answerReq.getFileCount())) {
						for (int j = 0; j < Integer.valueOf(answerReq.getFileCount()); j++) {
							count++;
							totalcount++;

							sum++;
						}
					}
				}
			}

			tempjson = HttpJsonAdapter.getInstance().toJson(answerReqBean);
			// Toast.makeText(this, tempjson, Toast.LENGTH_LONG).show();
			Log.v("qqqq", tempjson);
		} catch (Exception e) {
			e.printStackTrace();
		}
		end = "0";
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.common, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public View createView() {

		TextView tv = new TextView(this);
		tv.setText(Task_Name);
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		return tv;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated
															// method stub
		if (item.getItemId() == R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View createSpinnerView() {
		ImageView imageview = new ImageView(this);
		return imageview;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btnNext:
			success = 0;
			uploadAnswer();
			btnNext.setEnabled(false);
			break;
		case R.id.btnPrevious:
			AnswerFinishActivity.this.finish();
			break;
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	void uploadAnswer() {
		Log.i("jsc", "tempjson:"+tempjson);
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.uploadAnswer_url);
		params.put("did", SinofaithUtil.did);
		params.put("dajson", tempjson);
		if (totalcount == 0) {
			params.put("taskfinally", "1");
		}
		Log.v("qqqq", ConstansJsonUrl.REQUEST_URL + "?" + params);
		fh.post(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				if (!mLoadingDialog.isShowing()) {
					mLoadingDialog.show();
				}
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				try {
					BaseMessage baseMessage = HttpJsonAdapter.getInstance().get(t.toString(), BaseMessage.class);
					if (baseMessage.getResult().equals("0")) {
						if (count == 0) {
							mLoadingDialog.dismiss();
							QuestionAnswerDao answerDao = new QuestionAnswerDao(AnswerFinishActivity.this);
							// List<QuestionAnswerBean> answerlist = answerDao
							// .queryBySql("select * from "
							// + QuestionAnswerBean.TABLENAME
							// + " where Task_ID='" + answerBean.getTask_ID()
							// + "' and SubTask_ID='"
							// +
							// answerBean.getSubTask_ID()+"' and
							// Stemp_ID='"+answerBean.getStemp_ID()+"' and
							// Submiter='"+SinofaithUtil.userInfo.getUserMobile()+"'");

							String sql = "select * from " + QuestionAnswerBean.TABLENAME + " where Task_ID="
									+ answerBean.getTask_ID() + " and SubTask_ID=" + answerBean.getSubTask_ID();
							if (null != answerBean.getStemp_ID() && !"".equals(answerBean.getStemp_ID())) {
								sql += " and Stemp_ID=" + answerBean.getStemp_ID();
							}
							sql += " and Submiter=" + userMobile;
							List<QuestionAnswerBean> answerlist = answerDao.queryBySql(sql);
							if (null != answerlist && answerlist.size() > 0) {
								for (int i = 0; i < answerlist.size(); i++) {
									answerBean = answerlist.get(i);
									answerBean.setIsFinish("2");
									answerDao.update(answerBean);
								}
							}
							Toast.makeText(AnswerFinishActivity.this, "提交成功，谢谢参与！", Toast.LENGTH_LONG).show();
							AnswerFinishActivity.this.finish();
						} else {
							ArrayList<AnswerReq> answerReqs = answerReqBean.getSubAnswer();
							if (null != answerReqs && answerReqs.size() > 0) {
								int begin = 0;
								for (int i = 0; i < answerReqs.size(); i++) {
									final AnswerReq answerReq = answerReqs.get(i);
									if (null != answerReq.getFileCount() && !"".equals(answerReq.getFileCount())
											&& !"0".equals(answerReq.getFileCount())) {
										final String[] temps = answerReq.getAnswer().split("#");
										for (int j = 0; j < Integer.valueOf(answerReq.getFileCount()); j++) {
											Log.v("qqqq", temps[j]);
											if (null != temps[j] && !"".equals(temps[j])) {
												final File file = new File(temps[j]);
												final String temppath = temps[j];
												if (null != file && !"".equals(file)) {
													String fileName = file.getName();
													fileType = fileName.substring(fileName.lastIndexOf(".") + 1,
															fileName.length());
													if (j == (Integer.valueOf(answerReq.getFileCount()) - 1)) {
														end = "1";
													}

													if ("3".equals(answerReq.getQuestionType())) {
														qtype = "1";
													} else if ("4".equals(answerReq.getQuestionType())) {
														qtype = "2";
													} else {
														qtype = "3";
													}
													if (begin == 0 && count > 1) {
														firstParamsMap.put("sid", ConstansJsonUrl.uploadFile_url);
														firstParamsMap.put("did", SinofaithUtil.did);
														firstParamsMap.put("taskid", answerReqBean.getTask_ID());
														firstParamsMap.put("subtaskid", answerReqBean.getSubTask_ID());
														firstParamsMap.put("tempid", answerReqBean.getStemp_ID());
														firstParamsMap.put("questionid",
																answerReq.getTaskQuestion_ID());
														firstParamsMap.put("mobile", userMobile);
														firstParamsMap.put("ftype", qtype);
														firstParamsMap.put("suffix", fileType);
														firstParamsMap.put("order", answerReq.getOrder());
														firstParamsMap.put("desc", answerReq.getOtherAnswer());
														firstParamsMap.put("finally", "1");
														firstParamsMap.put("taskfinally", "1");
														firstParamsMap.put("GPS", answerReq.getGPS());
														firstParamsMap.put("path", temppath);
														totalcount--;
														begin++;
														continue;
													}
													mLoadingDialog.show();
													uploadFile(answerReqBean.getTask_ID(),
															answerReqBean.getSubTask_ID(), answerReqBean.getStemp_ID(),
															answerReq.getTaskQuestion_ID(), qtype, fileType,
															answerReq.getOrder(), answerReq.getOtherAnswer(), file,
															temppath, answerReq.getGPS());

												}
											}
										}
									}
								}
							}
						}

						// Toast.makeText(AnswerFinishActivity.this,
						// "上传成功，谢谢参与！", Toast.LENGTH_LONG).show();
						// AnswerFinishActivity.this.finish();

					} else {
						Toast.makeText(AnswerFinishActivity.this, baseMessage.getDesc(), Toast.LENGTH_LONG).show();
						btnNext.setEnabled(true);
					}
				} catch (Exception e) {
					Toast.makeText(AnswerFinishActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
					btnNext.setEnabled(true);
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(AnswerFinishActivity.this, strMsg, Toast.LENGTH_LONG).show();
				btnNext.setEnabled(true);
			}

		});
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	void uploadFile(String taskid, String subtaskid, String tempid, final String questionid, String ftype,
			String suffix, String order, String desc, File file, String path, String GPS) {
		if (totalcount > 0) {
			totalcount--;
		}
		FinalHttp fh = new FinalHttp();
		final AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.uploadFile_url);
		params.put("did", SinofaithUtil.did);
		params.put("taskid", taskid);
		params.put("subtaskid", subtaskid);
		params.put("tempid", tempid);
		params.put("questionid", questionid);
		params.put("mobile", userMobile);
		params.put("ftype", ftype);
		params.put("suffix", suffix);
		params.put("order", order);
		params.put("desc", desc);
		params.put("finally", end);
		if (count == 1) {
			params.put("taskfinally", "1");
		}
		params.put("GPS", GPS);
		try {
			S.FileParams(this, params, file);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		paramsMap.put("sid", ConstansJsonUrl.uploadFile_url);
		paramsMap.put("did", SinofaithUtil.did);
		paramsMap.put("taskid", taskid);
		paramsMap.put("subtaskid", subtaskid);
		paramsMap.put("tempid", tempid);
		paramsMap.put("questionid", questionid);
		paramsMap.put("mobile", userMobile);
		paramsMap.put("ftype", ftype);
		paramsMap.put("suffix", suffix);
		paramsMap.put("order", order);
		paramsMap.put("desc", desc);
		paramsMap.put("finally", end);
		if (count == 1) {
			paramsMap.put("taskfinally", "1");
		}
		paramsMap.put("GPS", SinofaithUtil.GPS);
		paramsMap.put("path", path);

		Log.v("qqqq", ConstansJsonUrl.REQUEST_URL + "?" + params);
		end = "0";
		fh.post(ConstansJsonUrl.REQUEST_URL, null, params, null, new AjaxCallBack() {
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				// if(!mLoadingDialog.isShowing()){
				// mLoadingDialog.show();
				// }
			}

			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				try {
					BaseMessage baseMessage = HttpJsonAdapter.getInstance().get(t.toString(), BaseMessage.class);
					if (baseMessage.getResult().equals("0")) {

						// if (count > 0) {
						count--;
						success++;
						// }
						if (count == 1 && null != firstParamsMap) {
							if (null != firstParamsMap.get("path")) {
								File file = new File(firstParamsMap.get("path"));
								if (null != file && file.exists()) {
									mLoadingDialog.show();
									end = "1";
									uploadFile(firstParamsMap.get("taskid"), firstParamsMap.get("subtaskid"),
											firstParamsMap.get("tempid"), firstParamsMap.get("questionid"),
											firstParamsMap.get("ftype"), firstParamsMap.get("suffix"),
											firstParamsMap.get("order"), firstParamsMap.get("desc"), file,
											firstParamsMap.get("path"), firstParamsMap.get("GPS"));
								}
							}

						}
						if (count == 0) {
							mLoadingDialog.dismiss();
							QuestionAnswerDao answerDao = new QuestionAnswerDao(AnswerFinishActivity.this);
							String sql = "select * from " + QuestionAnswerBean.TABLENAME + " where Task_ID="
									+ answerBean.getTask_ID() + " and SubTask_ID=" + answerBean.getSubTask_ID();
							if (null != answerBean.getStemp_ID() && !"".equals(answerBean.getStemp_ID())) {
								sql += " and Stemp_ID=" + answerBean.getStemp_ID();
							}
							sql += " and Submiter=" + userMobile;
							List<QuestionAnswerBean> answerlist = answerDao.queryBySql(sql);
							if (null != answerlist && answerlist.size() > 0) {
								for (int i = 0; i < answerlist.size(); i++) {
									answerBean = answerlist.get(i);
									answerBean.setIsFinish("2");
									answerDao.update(answerBean);
								}
							}
							// Toast.makeText(AnswerFinishActivity.this,
							// "提交成功，谢谢参与！", Toast.LENGTH_LONG).show();
							// AnswerFinishActivity.this.finish();
							showDialog(AnswerFinishActivity.this, sum, success);
						}
					} else {
						// mLoadingDialog.dismiss();
						// Log.v("error", baseMessage.getDesc());
						// Toast.makeText(AnswerFinishActivity.this,
						// baseMessage.getDesc()+"---"+questionid,
						// Toast.LENGTH_LONG).show();
						// btnNext.setEnabled(true);
						paramsList.add(paramsMap);
						// if(totalcount==0){
						// if(null==dialog || !dialog.isShowing()){
						reUpload();
						// }
						// }
					}
				} catch (Exception e) {
					// mLoadingDialog.dismiss();
					// Log.v("error", e.getMessage());
					// Toast.makeText(AnswerFinishActivity.this,
					// e.getMessage()+"==="+questionid,
					// Toast.LENGTH_LONG)
					// .show();
					// btnNext.setEnabled(true);
					paramsList.add(paramsMap);
					// if(totalcount==0){
					// if(null==dialog || !dialog.isShowing()){
					reUpload();
					// }
					// }
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				mLoadingDialog.dismiss();
				super.onFailure(t, errorNo, strMsg);
				// Toast.makeText(AnswerFinishActivity.this, strMsg,
				// Toast.LENGTH_LONG).show();
				// btnNext.setEnabled(true);
				paramsList.add(paramsMap);
				// if(totalcount==0){
				// if(null==dialog || !dialog.isShowing()){
				reUpload();
				// }
				// }
			}

		});
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	void reUpload() {
		fcount++;

		if (null != paramsList && paramsList.size() > 0) {
			mLoadingDialog.dismiss();
			if (null != dialog && dialog.isShowing()) {
				return;
			}
			MyDialog.Builder builder = new MyDialog.Builder(this);
			builder.setTitle("当前网络异常，请稍候尝试提交").setPositiveButton("确定",
					new android.content.DialogInterface.OnClickListener() {
						@Override
						public void onClick(final DialogInterface dialog111, int which) {
							// dialog.dismiss();
							// mLoadingDialog.show();
							// failcount=paramsList.size();
							// failtotalcount=paramsList.size();
							// for(int i=0;i<paramsList.size();i++){
							// final HashMap<String, String>
							// paramsMap=paramsList.get(i);
							// // paramsList.remove(i);
							// // i=i-1;
							// if(null!=paramsMap){
							// if(failtotalcount>0){
							// failtotalcount--;
							// }
							// FinalHttp fh = new FinalHttp();
							// final AjaxParams params = new AjaxParams();
							// params.put("sid", paramsMap.get("sid"));
							// params.put("did", paramsMap.get("did"));
							// params.put("taskid", paramsMap.get("taskid"));
							// params.put("subtaskid",
							// paramsMap.get("subtaskid"));
							// params.put("tempid", paramsMap.get("tempid"));
							// params.put("questionid",
							// paramsMap.get("questionid"));
							// params.put("mobile", paramsMap.get("mobile"));
							// params.put("ftype", paramsMap.get("ftype"));
							// params.put("suffix", paramsMap.get("suffix"));
							// params.put("order", paramsMap.get("order"));
							// params.put("desc", paramsMap.get("desc"));
							// params.put("finally", paramsMap.get("finally"));
							// if(failtotalcount==0){
							// params.put("taskfinally",
							// paramsMap.get("taskfinally"));
							// }
							// params.put("GPS", SinofaithUtil.GPS);
							// try {
							// // upfile=file;
							// // new
							// MyTask().execute(ConstansJsonUrl.REQUEST_URL+"?"+params,file.getAbsolutePath());
							// File file=new File(paramsMap.get("path"));
							// params.put("file", file);
							// } catch (Exception e1) {
							// // TODO Auto-generated catch block
							// e1.printStackTrace();
							// }
							// fh.post(ConstansJsonUrl.REQUEST_URL,null,
							// params,null, new AjaxCallBack() {
							// @Override
							// public void onStart() {
							// // TODO Auto-generated method stub
							// super.onStart();
							// // if(!mLoadingDialog.isShowing()){
							// // mLoadingDialog.show();
							// // }
							// }
							//
							// @Override
							// public void onLoading(long count, long current) {
							// // TODO Auto-generated method stub
							// super.onLoading(count, current);
							// }
							//
							// @Override
							// public void onSuccess(Object t) {
							// super.onSuccess(t);
							// try {
							// BaseMessage baseMessage = HttpJsonAdapter
							// .getInstance().get(t.toString(),
							// BaseMessage.class);
							// if (baseMessage.getResult().equals("0")) {
							// if(failcount>0){
							// failcount--;
							// }
							// if(failcount==0){
							// mLoadingDialog.dismiss();
							// QuestionAnswerDao answerDao = new
							// QuestionAnswerDao(
							// AnswerFinishActivity.this);
							// String sql = "select * from "
							// + QuestionAnswerBean.TABLENAME +
							// " where Task_ID="
							// + answerBean.getTask_ID() + " and SubTask_ID=" +
							// answerBean.getSubTask_ID();
							// if (null != answerBean.getStemp_ID() &&
							// !"".equals(answerBean.getStemp_ID())) {
							// sql += " and Stemp_ID=" +
							// answerBean.getStemp_ID();
							// }
							// sql += " and Submiter="
							// + SinofaithUtil.userInfo.getUserMobile();
							// List<QuestionAnswerBean> answerlist = answerDao
							// .queryBySql(sql);
							// if(null!=answerlist && answerlist.size()>0){
							// for(int i=0;i<answerlist.size();i++){
							// answerBean=answerlist.get(i);
							// answerBean.setIsFinish("2");
							// answerDao.update(answerBean);
							// }
							// }
							// Toast.makeText(AnswerFinishActivity.this,
							// "提交成功，谢谢参与！", Toast.LENGTH_LONG).show();
							// AnswerFinishActivity.this.finish();
							// }
							// lock.unlock();
							// } else {
							// // mLoadingDialog.dismiss();
							// // Log.v("error", baseMessage.getDesc());
							// // Toast.makeText(AnswerFinishActivity.this,
							// // baseMessage.getDesc()+"111",
							// // Toast.LENGTH_LONG).show();
							// // btnNext.setEnabled(true);
							// paramsList.add(paramsMap);
							// // if(totalcount==0){
							// // if(null==dialog || !dialog.isShowing()){
							// reUpload();
							// // }
							// // }
							// }
							// } catch (Exception e) {
							// // mLoadingDialog.dismiss();
							// // Log.v("error", e.getMessage());
							// // Toast.makeText(AnswerFinishActivity.this,
							// // e.getMessage()+"222", Toast.LENGTH_LONG)
							// // .show();
							// // btnNext.setEnabled(true);
							// paramsList.add(paramsMap);
							// // if(totalcount==0){
							// // if(null==dialog || !dialog.isShowing()){
							// reUpload();
							// // }
							// // }
							// }
							//
							// }
							//
							// @Override
							// public void onFailure(Throwable t, int errorNo,
							// String strMsg) {
							// mLoadingDialog.dismiss();
							// super.onFailure(t, errorNo, strMsg);
							// // Toast.makeText(AnswerFinishActivity.this,
							// strMsg,
							// // Toast.LENGTH_LONG).show();
							// // btnNext.setEnabled(true);
							// paramsList.add(paramsMap);
							// // if(totalcount==0){
							// // if(null==dialog || !dialog.isShowing()){
							// reUpload();
							// // }
							// // }
							// }
							//
							// });
							// }
							// }
							if (fcount >= 1) {
								if (dialog.isShowing()) {
									dialog.dismiss();
								}
								// Toast.makeText(AnswerFinishActivity.this,
								// "当前网络异常，请稍候尝试提交", Toast.LENGTH_LONG).show();
								// AnswerFinishActivity.this.finish();
								showDialog(AnswerFinishActivity.this, sum, success);
							}
						}
					});
			// .setNegativeButton(
			// "取消",
			// new android.content.DialogInterface.OnClickListener() {
			//
			// @Override
			// public void onClick(
			// DialogInterface dialog,
			// int which) {
			// dialog.dismiss();
			// dialog=null;
			// paramsList.clear();
			// btnNext.setEnabled(true);
			// lock.unlock();
			// return;
			// }
			// });
			dialog = builder.create();
			// if (dialog.isShowing()) {
			//
			// } else {
			// dialog.show();
			// }
			dialog.show();
		}
	}

	private String getSPString() {
		SharedPreferences sp = this.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}

	public static void showDialog(final Activity context, final int sum, final int success) {
		final String text = "总共" + sum + "个，成功" + success + "个，失败" + (sum - success) + "个";
		new AlertDialog.Builder(context).setMessage(text).setNeutralButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				context.finish();
			}
		}).setCancelable(false).show();
	}
}
